package com.recommend.recommendkeyword.controller;

import com.recommend.recommendkeyword.common.CommonResponse;
import com.recommend.recommendkeyword.entity.Comment;
import com.recommend.recommendkeyword.entity.User;
import com.recommend.recommendkeyword.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 注册功能实现
     */
    @PostMapping("/register")
    public CommonResponse<String> register(@RequestParam String username,@RequestParam String password,@RequestParam String email,@RequestParam int role){
        boolean status1 = userService.isExistUsername(username);
        if (status1)
            return CommonResponse.createForError("用户名已存在！");
        boolean status = userService.register(username, password, email, role);
        if (status)
            return CommonResponse.createForSuccess("注册成功");
        return CommonResponse.createForError("系统错误！");
    }
    /**
     * 登录功能实现
     */
    @PostMapping("/admin")
    public CommonResponse<String> login(@RequestParam String username,@RequestParam String password){
        boolean status = userService.login(username, password);
        if (status)
            return CommonResponse.createForSuccess("登陆成功!");
        return CommonResponse.createForError("账号或密码不正确!");
    }
    /**
     * 查看个人信息
     */
    @GetMapping("")
    public CommonResponse<User> getUser(@RequestParam int userId){
        User user = userService.getUser(userId);
        if (user == null)
            return CommonResponse.createForError("获取个人信息失败!");
        return CommonResponse.createForSuccess(user);
    }
    /**
     * 修改密码
     */
    @PutMapping("/password")
    public CommonResponse<String> changePassword(@RequestParam int userId,@RequestParam String oldPassword,
                                                 @RequestParam String newPassword){
        String changePasswordStr = userService.changePassword(userId, oldPassword, newPassword);
        if (changePasswordStr.equals("修改成功!"))
            return CommonResponse.createForSuccess(changePasswordStr);
        return CommonResponse.createForError(changePasswordStr);
    }
    /**
     * 购买VIP权限
     */
    @PutMapping("vip")
    public CommonResponse<String> purchaseVip(@RequestParam int userId){
        boolean status = userService.changeVip(userId);
        if (status)
            return CommonResponse.createForSuccess("调整VIP权限成功!");
        return CommonResponse.createForError("调整VIP权限失败!");
    }
    /**
     * 修改个人信息
     */
    @PutMapping("")
    public CommonResponse<String> changeUser(@RequestParam int userId,@RequestParam String newUsername,@RequestParam String newEmail){
        boolean status = userService.changeUser(userId, newUsername, newEmail);
        if (status)
            return CommonResponse.createForSuccess("修改成功!");
        return CommonResponse.createForError("系统错误!");
    }
}
